package cn.edu.jsu.zjj.model.utils.beanSearch;

import cn.zhxu.bs.FieldOp;
import cn.zhxu.bs.SqlWrapper;

import java.util.List;

public class LikeAND implements FieldOp {
    @Override
    public String name() { return "LikeAND"; }
    @Override
    public boolean isNamed(String name) {
        return "la".equals(name) || "likeAnd".equals(name);
    }
    @Override
    public boolean lonely() { return true; } // 返回 true 表示该运算符不需要参数值
    @Override
    public List<Object> operate(StringBuilder sqlBuilder, OpPara opPara) {
        SqlWrapper<Object> fieldSql = opPara.getFieldSql();
        Object[] values = opPara.getValues();
        List<Object> paras = fieldSql.getParas();
//        SELECT * FROM example_table WHERE description LIKE '%我%' AND description LIKE '%你%' AND description LIKE '%不是%';
        if(values!=null && values.length>0){
            StringBuilder conditionBuilder = new StringBuilder();
            for (int i = 0; i < values.length; i++) {
                String value = (String) values[i];
                if (i > 0) {
                    conditionBuilder.append(" AND ");
                }
                conditionBuilder.append(fieldSql.getSql()).append(" LIKE ?");
                paras.add("%" + value + "%");
            }

            sqlBuilder.append(conditionBuilder);
        }
        return paras;
    }
}
